﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EncuentraWEB
{
    public partial class Seguimiento2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var sUser = (string)Session["User"];
            if (sUser != "admin")
            {
                Response.Redirect("~\\default.aspx");
            }
            if (!IsPostBack)
            {
                cargarTreeView();
            }
            var scriptPopUp = new System.Text.StringBuilder();
            scriptPopUp.Append("PABLO();");
            ScriptManager.RegisterStartupScript(this, this.GetType(), "VENTANAPopUp", scriptPopUp.ToString(), true);


        }

        private void cargarTreeView()
        {
            int childnum = 0;
            int i = 0;


            var encuentraContext = new EncuentraEntities();
            encuentraContext.Connection.Open();
            var empresasquery = from emp in encuentraContext.Empresas
                                select new {emp.intIdEmpresa, emp.varDenominacion};
            foreach (var emp in empresasquery)
            {
                int empresa = emp.intIdEmpresa;
                var cochesquery = from coc in encuentraContext.Coches
                                  where coc.Empresas.intIdEmpresa == empresa
                                  select new {coc.intIdCoche, coc.varDominio};
                //int n = TreeView1.Nodes.Add(emp, emp, i);
                TreeNode n = new TreeNode(emp.varDenominacion, emp.intIdEmpresa.ToString(),
                                          "~//Imagenes//busnaranja16x16.png");
                n.Checked = true;
                TreeView1.Nodes.Add(n);
                i = TreeView1.Nodes.IndexOf(n);
                foreach (var coc in cochesquery)
                {
                    TreeNode child = new TreeNode(coc.varDominio, "coc" + coc.intIdCoche.ToString());
                    child.Checked = true;
                    TreeView1.Nodes[i].ChildNodes.Add(child);

                }
            }


        }
    }
}
